package com.pig4cloud.pig.module.controller.wx;

import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.property.PropertyFeeOrder;
import com.pig4cloud.pig.module.api.entity.wx.WxTemplate;
import com.pig4cloud.pig.module.api.entity.wx.WxUser;
import com.pig4cloud.pig.module.service.property.IPropertyFeeOrderService;
import com.pig4cloud.pig.module.service.wx.IWxTemplateService;
import com.pig4cloud.pig.module.service.wx.IWxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 微信公众号的模板消息列Controller
 *
 * @author ruoyi
 * @date 2021-08-11
 */
@Validated
@Api(value = "微信公众号的模板消息列控制器", tags = {"微信公众号的模板消息列管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wx/template")
public class WxTemplateController {

    private final IWxTemplateService wxTemplateService;
	private final IPropertyFeeOrderService propertyFeeOrderService;
	private final IWxUserService wxUserService;

    /**
     * 查询微信公众号的模板消息列列表
     */
    @ApiOperation("查询微信公众号的模板消息列列表")
    @PreAuthorize("@pms.hasPermission('module:wxtemplate:list')")
    @GetMapping("/list")
    public TableDataInfo<WxTemplate> list(@Validated WxTemplate wxTemplate) {
        return PageUtils.buildDataInfo(wxTemplateService.page(PageUtils.buildPage()));
    }

    /**
     * 获取微信公众号的模板消息列详细信息
     */
    @ApiOperation("获取微信公众号的模板消息列详细信息")
    @PreAuthorize("@pms.hasPermission('module:wxtemplate:query')")
    @GetMapping("/{id}")
    public R<WxTemplate> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        return R.ok(wxTemplateService.getById(id));
    }

	/**
	 * 修改物业费订单
	 */
	@ApiOperation("发送物业费订单")
	@GetMapping("/sendMsg/{orderId}")
	public R sendMsg(@NotNull(message = "主键不能为空") @PathVariable("orderId") Long orderId) {
		PropertyFeeOrder propertyFeeOrder = propertyFeeOrderService.findById(orderId);
		if(propertyFeeOrder != null){
			WxUser wxUser = wxUserService.findById(propertyFeeOrder.getUserId());
			if(wxUser!=null){
				wxTemplateService.sendWxMsgByPropertyFee(wxUser,propertyFeeOrder);
			}
		}
		return R.ok();
	}

    /**
     * 新增微信公众号的模板消息列
     */
    @ApiOperation("新增微信公众号的模板消息列")
    @PreAuthorize("@pms.hasPermission('module:wxtemplate:add')")
    @SysLog("微信公众号的模板消息列")
    @PostMapping()
    public R<Void> add(@Valid @RequestBody WxTemplate wxTemplate) {
        return R.okByRow(wxTemplateService.save(wxTemplate) ? 1 : 0);
    }

    /**
     * 修改微信公众号的模板消息列
     */
    @ApiOperation("修改微信公众号的模板消息列")
    @PreAuthorize("@pms.hasPermission('module:wxtemplate:edit')")
    @SysLog("微信公众号的模板消息列")
    @PutMapping()
    public R<Void> edit(@Valid @RequestBody WxTemplate wxTemplate) {
        return R.okByRow(wxTemplateService.updateById(wxTemplate) ? 1 : 0);
    }

    /**
     * 删除微信公众号的模板消息列
     */
    @ApiOperation("删除微信公众号的模板消息列")
    @PreAuthorize("@pms.hasPermission('module:wxtemplate:remove')")
    @SysLog("微信公众号的模板消息列")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return R.okByRow(wxTemplateService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
